var gulp = require("gulp");
var gulp_min_css = require("gulp-minify-css");
var gulp_sass = require("gulp-sass");
var gulp_plumber = require("gulp-plumber");
var gulp_tinypng = require("gulp-tinypng");
var gulp_uglify = require("gulp-uglify");
var browser_sync = require("browser-sync").create();
var gulpCopy = require("gulp-copy");
var gulp_autoprefixer = require("gulp-autoprefixer");

//定义gulp任务
gulp.task("mincss",function(){
	//这个任务具体要做的事情
	gulp.src('src/scss/**/*.scss')
		.pipe(gulp_plumber())
		.pipe(gulp_sass())
		.pipe(gulp_min_css({
			keepSpecialComments: '*'
		}))
		.pipe(gulp_autoprefixer({
			browsers: ['last 2 versions', 'Android >= 4.0','iOS 7','last 3 Safari versions'],
			cascade: false
		}))
		.pipe(gulp.dest('./dist/css'))
})

//实时监听scss文件并转换
gulp.task("watchscss",function () {
	gulp.watch('./scss/**/*.scss'["mincss"])
})


//定义图片的压缩任务
gulp.task("minimg",function() {
//	gulp.src('src/img/**/*')
//		.pipe(gulp_tinypng("F130o5pY96-2AezmpEp8EuYgyxXD0ufD"))
//		.pipe(gulp.dest('./dist/img'));
//	gulp.src('src/images/**/*')
//		.pipe(gulp_tinypng("F130o5pY96-2AezmpEp8EuYgyxXD0ufD"))
//		.pipe(gulp.dest('./dist/images'));
//	gulp.src('src/list_img/**/*')
//		.pipe(gulp_tinypng("F130o5pY96-2AezmpEp8EuYgyxXD0ufD"))
//		.pipe(gulp.dest('./dist/list_img'));
//	gulp.src('src/gouwuche_img/**/*')
//		.pipe(gulp_tinypng("F130o5pY96-2AezmpEp8EuYgyxXD0ufD"))
//		.pipe(gulp.dest('./dist/gouwuche_img'));
	gulp.src('src/login_img/**/*')
		.pipe(gulp_tinypng("F130o5pY96-2AezmpEp8EuYgyxXD0ufD"))
		.pipe(gulp.dest('./dist/login_img'));
		
});

//定义js的压缩任务
gulp.task("minjs",function(){
	gulp.src('src/js/**/*.js')
		.pipe(gulp_uglify())
		.pipe(gulp.dest('dist/js'))
});


gulp.task('copy', function() {
	gulp.src("*.html").pipe(gulpCopy("src"))
    gulp.src("./fonts/**/*").pipe(gulpCopy("src"))
})


//定义自动刷新浏览器任务
	gulp.task('browser_sync', function() {
    browser_sync.init({
    	port:Math.floor(Math.random()*9999)+1000,
        server: {
            baseDir: "./src"
        }
    });
    gulp.watch("scss/**/*").on("change",browser_sync.reload);
    gulp.watch("*.html").on("change",browser_sync.reload);
});

//定义一键开发模式
gulp.task("dev",["browser_sync"]);

//定义一键生成模式
gulp.task("pro",["mincss","minjs","minimg","copy"]);





